namespace SmartSoft.Lists
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.Controls.ChildGridControls;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormDmTs3Editor : FormListEditor
    {
        private IContainer components;
        private GridDetail grdDetail;
        private DataTable oDetailTable;
        private SearchColumn Column7;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private NumericColumn Column4;
        private NumericColumn Column5;
        private NumericColumn Column6;
        private DmTsDataAccess oMethod = new DmTsDataAccess();

        public FormDmTs3Editor()
        {
            this.InitializeComponent();
        }

        private bool AddNewRow()
        {
            if ((this.grdDetail.CurrentRow == null) || (this.grdDetail.CurrentRow.DataBoundItem == null))
            {
                return false;
            }
            if (((decimal) ((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row["So_Luong"]) == 0M)
            {
                return false;
            }
            if (this.grdDetail.CurrentRow.Index != (this.grdDetail.Rows.Count - 1))
            {
                return false;
            }
            this.grdDetail.EndEdit();
            DataRow row = this.oDetailTable.NewRow();
            row["Stt3"] = SSDataAccess.Cong_So_Tt(AppInstance.Subsidiary);
            row["So_Luong"] = 0;
            row["Gia"] = 0;
            row["Tien"] = 0;
            this.oDetailTable.Rows.Add(row);
            BindingSource dataSource = this.grdDetail.DataSource as BindingSource;
            dataSource.Position = dataSource.Find("Stt3", row["Stt3"]);
            return true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdDetail_CellBottomLeftLeaved(object sender, EventArgs e)
        {
            if (((this.grdDetail.Rows.Count == 1) || !string.IsNullOrEmpty(((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row["Dien_Giai"].ToString())) || !string.IsNullOrEmpty(base.ActiveControl.Text))
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.NextCell;
            }
            else
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.DeleteCurrentRow;
            }
        }

        private void grdDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex == 3) || (e.ColumnIndex == 4))
            {
                DataRow row = ((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row;
                if ((row["So_Luong"] != DBNull.Value) && (row["Gia"] != DBNull.Value))
                {
                    row["Tien"] = ((decimal) row["So_Luong"]) * ((decimal) row["Gia"]);
                    this.grdDetail.Refresh();
                }
            }
        }

        private void grdDetail_CellRightLeaved(object sender, EventArgs e)
        {
            if (this.AddNewRow())
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.FirstCell;
            }
            else
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.FirstCellNextRow;
            }
        }

        private void grdDetail_CurrentRowsRemoved(object sender, EventArgs e)
        {
            this.OnDetailDelete();
        }

        private void InitializeComponent()
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
            this.grdDetail = new SmartSoft.Controls.GridDetail();
            this.Column7 = new SmartSoft.Controls.ChildGridControls.SearchColumn();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new SmartSoft.Controls.ChildGridControls.NumericColumn();
            this.Column5 = new SmartSoft.Controls.ChildGridControls.NumericColumn();
            this.Column6 = new SmartSoft.Controls.ChildGridControls.NumericColumn();
            ((System.ComponentModel.ISupportInitialize)(this.grdDetail)).BeginInit();
            this.SuspendLayout();
            // 
            // lblPhan_Loai
            // 
            this.lblPhan_Loai.Location = new System.Drawing.Point(12, 265);
            // 
            // txtMa_Loai1
            // 
            this.txtMa_Loai1.Location = new System.Drawing.Point(123, 262);
            // 
            // txtMa_Loai2
            // 
            this.txtMa_Loai2.Location = new System.Drawing.Point(251, 262);
            // 
            // txtMa_Loai3
            // 
            this.txtMa_Loai3.Location = new System.Drawing.Point(379, 262);
            // 
            // grdDetail
            // 
            this.grdDetail.AllowUserToAddRows = false;
            this.grdDetail.AllowUserToDeleteRows = false;
            this.grdDetail.CheckAutoSize = true;
            this.grdDetail.CheckColor = false;
            this.grdDetail.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column7,
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6});
            this.grdDetail.Dock = System.Windows.Forms.DockStyle.Fill;
            this.grdDetail.Font = new System.Drawing.Font("Arial", 8.25F);
            this.grdDetail.Location = new System.Drawing.Point(0, 25);
            this.grdDetail.Name = "grdDetail";
            this.grdDetail.Size = new System.Drawing.Size(731, 271);
            this.grdDetail.TabIndex = 39;
            this.grdDetail.CellRightLeaved += new System.EventHandler(this.grdDetail_CellRightLeaved);
            this.grdDetail.CurrentRowsRemoved += new System.EventHandler(this.grdDetail_CurrentRowsRemoved);
            this.grdDetail.CellBottomLeftLeaved += new System.EventHandler(this.grdDetail_CellBottomLeftLeaved);
            this.grdDetail.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.grdDetail_CellEndEdit);
            // 
            // Column7
            // 
            this.Column7.DataPropertyName = "Ma_Vt";
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            this.Column7.DefaultCellStyle = dataGridViewCellStyle1;
            this.Column7.DestinationFields = "Ten_Vt,Dvt";
            this.Column7.DisabledForeColor = System.Drawing.Color.Blue;
            this.Column7.HeaderText = "Mã phụ tùng";
            this.Column7.MinimumWidth = 90;
            this.Column7.Name = "Column7";
            this.Column7.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column7.SourceFields = "Ten_Vt,Dvt";
            this.Column7.TableName = "DMVT";
            this.Column7.UserTextLength = SmartSoft.AppInstance.TextControlLength.ItemCode;
            this.Column7.UserTrimMode = SmartSoft.AppInstance.TextControlTrimMode.Right;
            this.Column7.Width = 90;
            // 
            // Column1
            // 
            this.Column1.DataPropertyName = "Ten_Vt";
            this.Column1.FillWeight = 93.08755F;
            this.Column1.HeaderText = "Tên phụ tùng";
            this.Column1.MinimumWidth = 50;
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.Column1.Width = 140;
            // 
            // Column2
            // 
            this.Column2.DataPropertyName = "Dien_Giai";
            this.Column2.HeaderText = "Mô tả";
            this.Column2.Name = "Column2";
            this.Column2.Width = 118;
            // 
            // Column3
            // 
            this.Column3.DataPropertyName = "Dvt";
            this.Column3.FillWeight = 39.63169F;
            this.Column3.HeaderText = "Đvt";
            this.Column3.MinimumWidth = 40;
            this.Column3.Name = "Column3";
            this.Column3.Width = 40;
            // 
            // Column4
            // 
            this.Column4.DataPropertyName = "So_Luong";
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column4.DefaultCellStyle = dataGridViewCellStyle2;
            this.Column4.FillWeight = 72.42487F;
            this.Column4.HeaderText = "Số lượng";
            this.Column4.MinimumWidth = 100;
            this.Column4.Name = "Column4";
            this.Column4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column4.UserTextNumericFormat = SmartSoft.AppInstance.TextNumericFormat.Quantity;
            // 
            // Column5
            // 
            this.Column5.DataPropertyName = "Gia";
            dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column5.DefaultCellStyle = dataGridViewCellStyle3;
            this.Column5.FillWeight = 95.47137F;
            this.Column5.HeaderText = "Đơn giá";
            this.Column5.MinimumWidth = 100;
            this.Column5.Name = "Column5";
            this.Column5.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column5.UserTextNumericFormat = SmartSoft.AppInstance.TextNumericFormat.LocalPrice;
            this.Column5.ZeroIsValid = true;
            // 
            // Column6
            // 
            this.Column6.DataPropertyName = "Tien";
            dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            this.Column6.DefaultCellStyle = dataGridViewCellStyle4;
            this.Column6.FillWeight = 97.05963F;
            this.Column6.HeaderText = "Thành tiền";
            this.Column6.MinimumWidth = 100;
            this.Column6.Name = "Column6";
            this.Column6.ReadOnly = true;
            this.Column6.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column6.UserTextNumericFormat = SmartSoft.AppInstance.TextNumericFormat.LocalAmount;
            // 
            // FormDmTs3Editor
            // 
            this.AddTitleText = "Dụng cụ, phụ tùng kèm theo";
            this.ClientSize = new System.Drawing.Size(731, 296);
            this.Controls.Add(this.grdDetail);
            this.EditTitleText = "Dụng cụ, phụ tùng kèm theo";
            this.MinimumSize = new System.Drawing.Size(737, 328);
            this.Name = "FormDmTs3Editor";
            this.ShowAddButton = false;
            this.ShowFreeFields = false;
            this.Text = "Dụng cụ, phụ tùng kèm theo";
            this.Controls.SetChildIndex(this.lblPhan_Loai, 0);
            this.Controls.SetChildIndex(this.txtMa_Loai1, 0);
            this.Controls.SetChildIndex(this.txtMa_Loai2, 0);
            this.Controls.SetChildIndex(this.txtMa_Loai3, 0);
            this.Controls.SetChildIndex(this.grdDetail, 0);
            ((System.ComponentModel.ISupportInitialize)(this.grdDetail)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public void OnDetailDelete()
        {
            if (MessageBox.Show(AppInstance.DeleMessage, AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.DeleteCurrentRow;
            }
        }

        public override bool OnSave()
        {
            this.oMethod.Save3(base.LocalCurrentRow[base.LocalFormLister.LocalKeyField], this.oDetailTable);
            return true;
        }

        public override void OnShow()
        {
            this.oDetailTable = this.oMethod.GetDetailTable3(base.LocalCurrentRow[base.LocalFormLister.LocalKeyField].ToString());
            this.grdDetail.DataSource = new BindingSource(this.oDetailTable, string.Empty);
            if (this.oDetailTable.Rows.Count == 0)
            {
                DataRow row = this.oDetailTable.NewRow();
                row["Stt3"] = SSDataAccess.Cong_So_Tt(AppInstance.Subsidiary);
                row["So_Luong"] = 0;
                row["Gia"] = 0;
                row["Tien"] = 0;
                this.oDetailTable.Rows.Add(row);
            }
        }
    }
}

